package com.sunyard.client2.ui;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import com.sunyard.client.SunEcmClientApi;
import com.sunyard.client.bean.ClientBatchBean;
import com.sunyard.client.bean.ClientBatchFileBean;
import com.sunyard.client.bean.ClientBatchIndexBean;
import com.sunyard.client.bean.ClientFileBean;
import com.sunyard.client2.ConnectionFactory;
import com.sunyard.client2.conf.ConfUtil;
import com.sunyard.util.DateUtil;

public class Upload extends JPanel {
	private JTextField textField;
	private JTable table;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;

	/**
	 * Create the panel.
	 */
	public Upload() {
		setLayout(null);

		JButton btnNewButton = new JButton("上传");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int count = Integer.parseInt(textField_5.getText());
					long start = System.currentTimeMillis();
					for (int j = 0; j < count; j++) {
						ClientBatchBean clientBatchBean = new ClientBatchBean();
						clientBatchBean.setModelCode(ConfUtil.getString("modelCode", ""));
						clientBatchBean.setUser(ConfUtil.getString("userName", ""));
						clientBatchBean.setPassWord(ConfUtil.getString("passWord", ""));
						
						clientBatchBean.setToken_code(ConfUtil.getString("token_value", ""));
						clientBatchBean.setBreakPoint(true); // 是否作为断点续传上传
						clientBatchBean.setOwnMD5(true); // 是否为批次下的文件添加MD5码
						// =========================设置索引对象信息开始=========================
						ClientBatchIndexBean clientBatchIndexBean = new ClientBatchIndexBean();
						String id = textField_1.getText();
						if (id != null && !id.equals("")) {
							clientBatchIndexBean.setContentID(id);
						}
						

						// 索引自定义属性
						clientBatchIndexBean.addCustomMap(textField_3.getText(), textField_4.getText());

						// =========================设置文档部件信息开始=========================
						ClientBatchFileBean clientBatchFileBeanA = new ClientBatchFileBean();
						clientBatchFileBeanA.setFilePartName(ConfUtil.getString("filePartName", ""));
						// =========================设置文档部件信息结束=========================

						// =========================添加文件=========================
						File path = new File(textField.getText());
						File[] files = path.listFiles();
						int i = 0;
						for (; i < files.length; i++) {
							if (files[i].isDirectory()) {
								continue;
							}
							ClientFileBean fileBean1 = new ClientFileBean();
							fileBean1.setFileName(files[i].getAbsolutePath());
							fileBean1.setFileFormat(files[i].getName().substring(
									files[i].getName().lastIndexOf(".") + 1));
							fileBean1.setFilesize(String.valueOf(files[i].length()));
							if(files[i].getName().equals("com.jpg")){
								fileBean1.setFilesize("195363");
							}
							clientBatchFileBeanA.addFile(fileBean1);
						}
						clientBatchIndexBean.setAmount(textField_2.getText());
						// =========================添加文件=========================
						clientBatchBean.setIndex_Object(clientBatchIndexBean);
						clientBatchBean.addDocument_Object(clientBatchFileBeanA);
						SunEcmClientApi clientApi = ConnectionFactory.getConnection();

						String resultMsg = clientApi.upload(clientBatchBean, ConfUtil.getString("groupName", ""));
						ResultMessageBox.getInstance().showMessage(resultMsg);
					}
					long end = System.currentTimeMillis();
					System.out.println("上传总耗时:"+(end-start)+"ms");
				} catch (Exception e1) {
					ResultMessageBox.getInstance().showMessage(e1.getMessage());
				}
			}
		});
		btnNewButton.setBounds(595, 295, 93, 34);
		add(btnNewButton);

		JLabel lblNewLabel = new JLabel("文件夹：");
		lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel.setBounds(39, 10, 64, 23);
		add(lblNewLabel);

		JButton btnNewButton_1 = new JButton("选择");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fc = new JFileChooser();
				fc.setCurrentDirectory(new File(ConfUtil.getString("dir", "C:/")));
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnVal = fc.showOpenDialog(null);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					textField.setText(file.getPath());
					File[] fs = file.listFiles();
					DefaultTableModel m = (DefaultTableModel) table.getModel();
					m.setRowCount(0);
					for (int i = 0; i < fs.length; i++) {
						String name = fs[i].getName();
						String format = name.substring(name.lastIndexOf(".") + 1);
						m.addRow(new Object[] { i + 1, name, format, (fs[i].length() / 1024) + "K" });
					}
					textField_2.setText(String.valueOf(fs.length));
				}
			}
		});
		btnNewButton_1.setBounds(503, 10, 78, 23);
		add(btnNewButton_1);

		textField = new JTextField();
		textField.setBounds(113, 10, 380, 23);
		add(textField);
		textField.setColumns(10);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(39, 43, 649, 227);
		add(scrollPane);

		table = new JTable(new DefaultTableModel(new Object[][] { { null, null, null, null, } }, new String[] { "序号",
				"文件名", "格式", "大小" }) {
			@Override
			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return false;
			}

			@Override
			public void setValueAt(Object aValue, int row, int column) {
				super.setValueAt(aValue, row, column);
			}
		});

		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
					boolean hasFocus, int row, int column) {
				if (column == 0) {
					this.setHorizontalAlignment(CENTER);
				} else if (column == 1) {
					this.setHorizontalAlignment(LEFT);
				} else if (value instanceof String) {
					this.setHorizontalAlignment(CENTER);
				} else if (value instanceof Float || value instanceof Integer || value instanceof Long) {
					this.setHorizontalAlignment(RIGHT);
				}

				return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
			}
		};

		for (int i = 0; i < table.getColumnCount(); i++) {
			table.getColumn(table.getModel().getColumnName(i)).setCellRenderer(tcr);
		}
		table.setCursor(new Cursor(Cursor.HAND_CURSOR));
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		table.getColumnModel().getColumn(0).setPreferredWidth(15);
		table.getColumnModel().getColumn(1).setPreferredWidth(160);
		table.getColumnModel().getColumn(2).setPreferredWidth(50);
		table.getColumnModel().getColumn(3).setPreferredWidth(50);

		scrollPane.setViewportView(table);

		JLabel label = new JLabel("批次编号：");
		label.setHorizontalAlignment(SwingConstants.LEFT);
		label.setBounds(39, 280, 64, 23);
		add(label);

		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(128, 280, 457, 23);
		add(textField_1);

		textField_2 = new JTextField();
		textField_2.setBounds(598, 11, 78, 21);
		add(textField_2);
		textField_2.setColumns(10);

		JLabel label_1 = new JLabel("时间字段：");
		label_1.setHorizontalAlignment(SwingConstants.LEFT);
		label_1.setBounds(39, 316, 76, 15);
		add(label_1);

		textField_3 = new JTextField();
		textField_3.setText(ConfUtil.getString("STARTCOLUMN", ""));
		textField_3.setColumns(10);
		textField_3.setBounds(128, 313, 140, 21);
		add(textField_3);

		JLabel label_2 = new JLabel("业务时间：");
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setBounds(359, 313, 76, 15);
		add(label_2);

		textField_4 = new JTextField();
		textField_4.setText(ConfUtil.getString("STARTDATE", DateUtil.get8bitDateStr()));
		textField_4.setColumns(10);
		textField_4.setBounds(445, 310, 140, 21);
		add(textField_4);

		JLabel label_3 = new JLabel("循环次数：");
		label_3.setBounds(698, 45, 87, 15);
		add(label_3);

		textField_5 = new JTextField();
		textField_5.setBounds(698, 70, 87, 21);
		textField_5.setText("1");
		add(textField_5);
		textField_5.setColumns(10);

	}
}
